package com.tang.gulimall.gateway.Config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsConfigurationSource;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;
import org.springframework.web.server.ServerWebExchange;

@Configuration  //这是一个配置类
public class GulimallCorsfiguration {

    //springboot自带的跨域filter
    @Bean
    public CorsWebFilter corsWebFilter(){

        //使用响应式包下的类 reactive
        UrlBasedCorsConfigurationSource Source = new UrlBasedCorsConfigurationSource();

        CorsConfiguration configuration = new CorsConfiguration();
        //配置跨域信息
        configuration.addAllowedHeader("*");    //允许那些头
        configuration.addAllowedMethod("*");   //允许那些请求方式
        configuration.addAllowedOrigin("*");     //允许所有请求来源
        configuration.setAllowCredentials(true);   //是否允许携带cookie信息进行跨域,否则会丢失cookie信息

        Source.registerCorsConfiguration("/**",configuration); //任意路径下都要跨域

        return new CorsWebFilter(Source);
    }
}
